package com.hnkywl.sts.action.tjbb;

import com.hnkywl.sts.dao.djgl.JyddDao;
import com.hnkywl.sts.entity.djgl.Jydd;
import com.hnkywl.sts.service.tjbb.Ddcxservice;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by HL on 2015/9/20.
 */
@Namespace("/tjbb/ddcx")
public class DdcxAction extends SimpleActionSupport {
    private Ddcxservice ddcxservice;
    private int page;
    private int rows;
    private String htbm;
    private Long id;
    private Jydd jydd;

    private JyddDao jyddDao;


    @Action("ddcxList")
    public void bzjyjcList() {
        this.renderJson(this.ddcxservice.findAlljydd(htbm, page, rows));
    }


    @Action("ddcx_detail")
    public String ddcx_detail() {
        jydd = ddcxservice.findJyddById(id);
        return SUCCESS;
    }

    @Action("ddcxdetail_goodsesmx")
    public void goodsesList() {
        this.renderJson(this.ddcxservice.findGoodsesByJydd(id, page, 9999999));
    }


    @Action("ddcx_export")
    public String ddcxExport() {
        //获取订单查询数据
        List<Jydd> jyddlist = jyddDao.getAllJydds();

        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("交易订单");
        POIUtil poiUtil = new POIUtil(wb);


        //设置列宽
        sheet.setDefaultRowHeightInPoints(15);
        sheet.setDefaultColumnWidth((short) 17);
        sheet.setColumnWidth((short) 0, (short) (11 * 512));
        sheet.setColumnWidth((short) 1, (short) (9 * 512));


        //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        row.setHeightInPoints(36);

        //第四步，创建单元格，并设置值表头  设置表头居中
        HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(wb);
        HSSFCell titleCell = row.createCell((short) 0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        titleCell.setCellValue("交易订单导出汇总表");

        // 四个参数分别是：起始行，起始列，结束行，结束列
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 1));
        row = sheet.createRow((int) 1);
        row.setHeightInPoints(21);

        //生成第一行数据
        HSSFCell jyddh = row.createCell((short) 0);
        jyddh.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        jyddh.setEncoding(HSSFCell.ENCODING_UTF_16);
        jyddh.setCellValue("交易订单号");

        HSSFCell htbh = row.createCell((short) 1);
        htbh.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        htbh.setEncoding(HSSFCell.ENCODING_UTF_16);
        htbh.setCellValue("合同编号");
        //表体部分
        for (int i = 2; i < jyddlist.size() + 2; i++) {
            row = sheet.createRow(i);
            row.setHeightInPoints(19);

            jyddh = row.createCell((short) 0);
            jyddh.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            jyddh.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != jyddlist.get(i - 2).getOrder_id()) {
                jyddh.setCellValue(jyddlist.get(i - 2).getOrder_id());
            }

            htbh = row.createCell((short) 0);
            htbh.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            htbh.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != jyddlist.get(i - 2).getOrder_sn()) {
                htbh.setCellValue(jyddlist.get(i - 2).getOrder_sn());
            }
        }


        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("采购明细汇总表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            // wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;

    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Jydd getJydd() {
        return jydd;
    }

    public void setJydd(Jydd jydd) {
        this.jydd = jydd;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getHtbm() {
        return htbm;
    }

    public void setHtbm(String htbm) {
        this.htbm = htbm;
    }

    @Autowired
    public void setDdcxservice(Ddcxservice ddcxservice) {
        this.ddcxservice = ddcxservice;
    }

    @Autowired
    public void setJyddDao(JyddDao jyddDao) {
        this.jyddDao = jyddDao;
    }


}
